package com.haixiaoke.flat.apartment.controller;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.apartment.domain.HousingAllocation;
import com.haixiaoke.saas.apartment.service.IHousingAllocationService;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.domain.SysArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房屋配置Controller
 *
 * @author panda
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/front/apartment/allocation")
public class FlatHousingAllocationController extends BaseController {

    @Autowired
    private IHousingAllocationService housingAllocationService;

    /**
     * 小程序首页筛选分页
     * @param allocation
     * @return
     */
    @GetMapping("/homePage/search")
    public TableDataInfo homePageSearch(HousingAllocation allocation) {
        startPage();
        if (allocation.getStartTime() != null && allocation.getEndTime() != null) {
            if (allocation.getStartTime().after(allocation.getEndTime())) {
                throw new ServiceException("时间格式错误");
            }
        }
        List<HousingAllocation> list = housingAllocationService.selectHousingAllocationHomePage(allocation);
        return getDataTable(list);
    }


    /**
     * 小程序首页筛选分页
     * @param allocation
     * @return
     */
    @GetMapping("/homePage")
    public TableDataInfo homePage(HousingAllocation allocation) {
        startPage();
        List<HousingAllocation> list = housingAllocationService.selectHousingAllocationHomePageList(allocation);
        return getDataTable(list);
    }



    /**
     * 小程序首页城市列表
     *
     * @param
     * @return
     */
    @GetMapping("/selectArea")
    public AjaxResult selectArea() {
        List<SysArea> list = housingAllocationService.selectHousingAllocationArea();
        return success(list);
    }

    /**
     * 公寓查看房间详情
     */
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody HousingAllocation allocation) {
        return success(housingAllocationService.flatSelectHousingAllocationByHouseId(allocation));
    }


    /**
     * 预估公寓房屋价格
     * @param allocation
     * @return
     */
    @PostMapping("/estimatedAmount")
    public AjaxResult estimatedAmount(@RequestBody HousingAllocation allocation){
        if (ObjectUtil.isEmpty(allocation.getStartTime()) || ObjectUtil.isEmpty(allocation.getEndTime())) {
            return error("请选择时间!");
        }
        if (allocation.getStartTime().after(allocation.getEndTime())){
            return error("时间数据错误!");
        }
        return success(housingAllocationService.flatestimatedAmount(allocation));
    }

    /**
     * 预估公寓退订价格
     * @param allocation
     * @return
     */
    @PostMapping("/unsubscribeAmount")
    public AjaxResult unsubscribeAmount(@RequestBody HousingAllocation allocation){
        if (ObjectUtil.isEmpty(allocation.getStartTime()) || ObjectUtil.isEmpty(allocation.getEndTime()) || ObjectUtil.isEmpty(allocation.getHouseId())) {
            return error("数据异常，请选择时间段和房屋!");
        }
        if (allocation.getStartTime().after(allocation.getEndTime())){
            return error("时间数据错误!");
        }
        return success(housingAllocationService.unsubscribeAmount(allocation));
    }
}
